using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentLastSchoolHistoryTableDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentLastSchoolHistoryTable objects.
    /// </summary>
    public class StudentLastSchoolHistoryTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentLastSchoolHistoryTable from the underlying datasource.
        /// </summary>
        /// <param name="userAccountKey">The unique UserAccountKey of the StudentLastSchoolHistoryTable in the database.</param>
        /// <returns>An StudentLastSchoolHistoryTable when the UserAccountKey was found in the database, or null otherwise.</returns>
        public static StudentLastSchoolHistoryTable GetItem(int SLSHKey, int userAccountKey)
        {
            StudentLastSchoolHistoryTable myStudentLastSchoolHistoryTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentLastSchoolHistoryTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@SLSHKey", SLSHKey);
                    myCommand.Parameters.AddWithValue("@userAccountKey", userAccountKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentLastSchoolHistoryTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentLastSchoolHistoryTable;
            }
        }

        /// <summary>
        /// Returns a list with StudentLastSchoolHistoryTable objects.
        /// </summary>
        /// <returns>A generics List with the StudentLastSchoolHistoryTable objects.</returns>
        public static StudentLastSchoolHistoryTableList GetList()
        {
            StudentLastSchoolHistoryTableList tempList = new StudentLastSchoolHistoryTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentLastSchoolHistoryTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a StudentLastSchoolHistoryTable in the database.
        /// </summary>
        /// <param name="myStudentLastSchoolHistoryTable">The StudentLastSchoolHistoryTable instance to save.</param>
        /// <returns>The new UserAccountKey if the StudentLastSchoolHistoryTable is new in the database or the existing UserAccountKey when an item was updated.</returns>
        public static int Save(StudentLastSchoolHistoryTable myStudentLastSchoolHistoryTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentLastSchoolHistoryTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentLastSchoolHistoryTable.SLSHKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@SLSHKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@SLSHKey", myStudentLastSchoolHistoryTable.SLSHKey);
                    }

                    if (myStudentLastSchoolHistoryTable.UserAccountKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", myStudentLastSchoolHistoryTable.UserAccountKey);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.LastSchoolStudentID))
                    {
                        myCommand.Parameters.AddWithValue("@lastSchoolStudentID", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@lastSchoolStudentID", myStudentLastSchoolHistoryTable.LastSchoolStudentID);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.SchoolName))
                    {
                        myCommand.Parameters.AddWithValue("@schoolName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolName", myStudentLastSchoolHistoryTable.SchoolName);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.ProgramName))
                    {
                        myCommand.Parameters.AddWithValue("@programName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@programName", myStudentLastSchoolHistoryTable.ProgramName);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.Affiliation))
                    {
                        myCommand.Parameters.AddWithValue("@affiliation", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@affiliation", myStudentLastSchoolHistoryTable.Affiliation);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.FromStandardLevel))
                    {
                        myCommand.Parameters.AddWithValue("@fromStandardLevel", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@fromStandardLevel", myStudentLastSchoolHistoryTable.FromStandardLevel);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.ToStandardLevel))
                    {
                        myCommand.Parameters.AddWithValue("@toStandardLevel", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@toStandardLevel", myStudentLastSchoolHistoryTable.ToStandardLevel);
                    }
                    if (myStudentLastSchoolHistoryTable.StartDate == null)
                    {
                        myCommand.Parameters.AddWithValue("@startDate", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@startDate", myStudentLastSchoolHistoryTable.StartDate);
                    }
                    if (myStudentLastSchoolHistoryTable.EndDate == null)
                    {
                        myCommand.Parameters.AddWithValue("@endDate", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@endDate", myStudentLastSchoolHistoryTable.EndDate);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.LastGrade))
                    {
                        myCommand.Parameters.AddWithValue("@lastGrade", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@lastGrade", myStudentLastSchoolHistoryTable.LastGrade);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.SchoolWebsiteUrl))
                    {
                        myCommand.Parameters.AddWithValue("@schoolWebsiteUrl", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolWebsiteUrl", myStudentLastSchoolHistoryTable.SchoolWebsiteUrl);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.SchoolEmail))
                    {
                        myCommand.Parameters.AddWithValue("@schoolEmail", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolEmail", myStudentLastSchoolHistoryTable.SchoolEmail);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.SchoolContact))
                    {
                        myCommand.Parameters.AddWithValue("@schoolContact", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolContact", myStudentLastSchoolHistoryTable.SchoolContact);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.UnitNo))
                    {
                        myCommand.Parameters.AddWithValue("@unitNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@unitNo", myStudentLastSchoolHistoryTable.UnitNo);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.StreetName))
                    {
                        myCommand.Parameters.AddWithValue("@streetName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@streetName", myStudentLastSchoolHistoryTable.StreetName);
                    }
                    if (myStudentLastSchoolHistoryTable.CityCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", myStudentLastSchoolHistoryTable.CityCode);
                    }
                    if (myStudentLastSchoolHistoryTable.DistrictCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", myStudentLastSchoolHistoryTable.DistrictCode);
                    }
                    if (myStudentLastSchoolHistoryTable.TownshipCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@townshipCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@townshipCode", myStudentLastSchoolHistoryTable.TownshipCode);
                    }
                    if (myStudentLastSchoolHistoryTable.CountryCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", myStudentLastSchoolHistoryTable.CountryCode);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.PostalCode))
                    {
                        myCommand.Parameters.AddWithValue("@postalCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@postalCode", myStudentLastSchoolHistoryTable.PostalCode);
                    }
                    if (string.IsNullOrEmpty(myStudentLastSchoolHistoryTable.Remark))
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", myStudentLastSchoolHistoryTable.Remark);
                    }
                    if (myStudentLastSchoolHistoryTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myStudentLastSchoolHistoryTable.Status);
                    }
                    if (myStudentLastSchoolHistoryTable.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", myStudentLastSchoolHistoryTable.CreatedDateTime);
                    }
                    if (myStudentLastSchoolHistoryTable.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentLastSchoolHistoryTable.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentLastSchoolHistoryTable from the database.
        /// </summary>
        /// <param name="userAccountKey">The UserAccountKey of the StudentLastSchoolHistoryTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int userAccountKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentLastSchoolHistoryTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@userAccountKey", userAccountKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentLastSchoolHistoryTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentLastSchoolHistoryTable FillDataRecord(IDataRecord myDataRecord)
        {
            StudentLastSchoolHistoryTable myStudentLastSchoolHistoryTable = new StudentLastSchoolHistoryTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SLSHKey")))
            {
                myStudentLastSchoolHistoryTable.SLSHKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SLSHKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
            {
                myStudentLastSchoolHistoryTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LastSchoolStudentID")))
            {
                myStudentLastSchoolHistoryTable.LastSchoolStudentID = myDataRecord.GetString(myDataRecord.GetOrdinal("LastSchoolStudentID"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolName")))
            {
                myStudentLastSchoolHistoryTable.SchoolName = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ProgramName")))
            {
                myStudentLastSchoolHistoryTable.ProgramName = myDataRecord.GetString(myDataRecord.GetOrdinal("ProgramName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Affiliation")))
            {
                myStudentLastSchoolHistoryTable.Affiliation = myDataRecord.GetString(myDataRecord.GetOrdinal("Affiliation"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FromStandardLevel")))
            {
                myStudentLastSchoolHistoryTable.FromStandardLevel = myDataRecord.GetString(myDataRecord.GetOrdinal("FromStandardLevel"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ToStandardLevel")))
            {
                myStudentLastSchoolHistoryTable.ToStandardLevel = myDataRecord.GetString(myDataRecord.GetOrdinal("ToStandardLevel"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StartDate")))
            {
                myStudentLastSchoolHistoryTable.StartDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("StartDate"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EndDate")))
            {
                myStudentLastSchoolHistoryTable.EndDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("EndDate"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LastGrade")))
            {
                myStudentLastSchoolHistoryTable.LastGrade = myDataRecord.GetString(myDataRecord.GetOrdinal("LastGrade"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolWebsiteUrl")))
            {
                myStudentLastSchoolHistoryTable.SchoolWebsiteUrl = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolWebsiteUrl"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolEmail")))
            {
                myStudentLastSchoolHistoryTable.SchoolEmail = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolEmail"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolContact")))
            {
                myStudentLastSchoolHistoryTable.SchoolContact = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolContact"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UnitNo")))
            {
                myStudentLastSchoolHistoryTable.UnitNo = myDataRecord.GetString(myDataRecord.GetOrdinal("UnitNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StreetName")))
            {
                myStudentLastSchoolHistoryTable.StreetName = myDataRecord.GetString(myDataRecord.GetOrdinal("StreetName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityCode")))
            {
                myStudentLastSchoolHistoryTable.CityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CityCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
            {
                myStudentLastSchoolHistoryTable.DistrictCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DistrictCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipCode")))
            {
                myStudentLastSchoolHistoryTable.TownshipCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TownshipCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
            {
                myStudentLastSchoolHistoryTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PostalCode")))
            {
                myStudentLastSchoolHistoryTable.PostalCode = myDataRecord.GetString(myDataRecord.GetOrdinal("PostalCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                myStudentLastSchoolHistoryTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myStudentLastSchoolHistoryTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentLastSchoolHistoryTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStudentLastSchoolHistoryTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStudentLastSchoolHistoryTable;
        }
    }

}
